package databases

import (
	"database/sql"
	"fmt"

	_ "github.com/go-sql-driver/mysql"
)

var userName = "root"
var password = "aixiaohu12685"
var ip = "127.0.0.1"
var port = "3306"
var database = "component"

var Sql_db *sql.DB
var Sql_err error

func init() {
	fmt.Println("databases int初始化\n------------\n-")
	InitDB()
}

func InitDB() {
	//构建连接："用户名:密码@tcp(IP:端口)/数据库?charset=utf8"
	DB, _err := sql.Open("mysql", userName+":"+password+"@tcp("+ip+":"+port+")/"+database+"?charset=utf8")
	//设置数据库最大连接数
	DB.SetConnMaxLifetime(100)
	//设置上数据库最大闲置连接数
	DB.SetMaxIdleConns(10)
	//验证连接
	if err := DB.Ping(); err != nil {
		Sql_err = _err
		fmt.Println("open database fail", Sql_err)
		return
	}
	Sql_db = DB
	fmt.Println("databases InitDB\n------------\n-")
}
